import {Buffer} from "buffer";
import {Unsigned16} from "./Unsigned16";
import {Unsigned8} from "./Unsigned8";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";


export class Date_time implements AxdrType{

    dataCode: Buffer | null = null;
    year: Unsigned16 | null = null;
    month: Unsigned8 | null = null;
    day_of_month: Unsigned8 | null = null;
    day_of_week: Unsigned8 | null = null;
    hour: Unsigned8 | null = null;
    minute: Unsigned8 | null = null;
    second: Unsigned8 | null = null;
    milliseconds: Unsigned8 | null = null;

    constructor() {
    }

    set_dataCode(dataCode: Buffer) {
        this.dataCode = dataCode;
    }

    set_date_time(year: Unsigned16, month: Unsigned8, day_of_month: Unsigned8,
                  day_of_week : Unsigned8, hour: Unsigned8, minute: Unsigned8,
                  second: Unsigned8, milliseconds : Unsigned8) {
        this.year = year;
        this.month = month;
        this.day_of_month = day_of_month;
        this.day_of_week = day_of_week;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        this.milliseconds = milliseconds;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.year = new Unsigned16();
        this.year.set_const()
        codeLength += this.year.decode(input);

        this.month = new Unsigned8();
        this.month.set_const()
        codeLength += this.month.decode(input);

        this.day_of_month = new Unsigned8();
        this.day_of_month.set_const()
        codeLength += this.day_of_month.decode(input);

        this.day_of_week = new Unsigned8();
        this.day_of_week.set_const()
        codeLength += this.day_of_week.decode(input);

        this.hour = new Unsigned8();
        this.hour.set_const()
        codeLength += this.hour.decode(input);

        this.minute = new Unsigned8();
        this.minute.set_const()
        codeLength += this.minute.decode(input);

        this.second = new Unsigned8();
        this.second.set_const()
        codeLength += this.second.decode(input);

        this.milliseconds = new Unsigned16();
        this.milliseconds.set_const()
        codeLength += this.milliseconds.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.year != null && this.month != null && this.day_of_week != null
                && this.day_of_month != null && this.hour != null && this.minute != null
                && this.second != null){
                codeLength = 0;
                codeLength += this.milliseconds.encode(output);
                codeLength += this.second.encode(output);
                codeLength += this.minute.encode(output);
                codeLength += this.hour.encode(output);
                codeLength += this.day_of_week.encode(output);
                codeLength += this.day_of_month.encode(output);
                codeLength += this.month.encode(output);
                codeLength += this.year.encode(output);
            }else{
                throw new Error("date_time is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString() : string{
        if(this.year != null && this.month != null && this.day_of_month != null
            && this.day_of_week != null && this.hour != null && this.minute != null
            && this.second != null){
            return "sequence: {"+ "year: " + this.year + ", month: " + this.month + ", day_of_month: " + this.day_of_month
                + ", day_of_week" + this.day_of_week + ", hour: " + this.hour + ", minute: " + this.minute + ", second: " + this.second  + "}";
        }else{
            return "date_time is null";
        }
    }

}
